<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Hoodoo::Presenters::Base</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>
    <div class="banner">
        <h1>
            <span class="type">Class</span>
            Hoodoo::Presenters::Base
                <span class="parent">&lt;
                    Object
                </span>
        </h1>
        <ul class="files">
            <li><a href="../../../files/lib/hoodoo/presenters/base_rb.html">lib/hoodoo/presenters/base.rb</a></li>
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
    <div class="description">
      
<p><a href="Base.html">Base</a> functionality for JSON validation and
presenter (rendering) layers. Subclass this to define a schema against
which validation of inbound data or rendering of outbound data can be
performed. Call schema in the subclass to declare, via the DSL, the shape
of the schema.</p>

    </div>








    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
        <dt>G</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-get_schema">get_schema</a>,
              </li>
              <li>
                <a href="#method-c-get_schema_definition">get_schema_definition</a>
              </li>
          </ul>
        </dd>
        <dt>I</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-is_internationalised-3F">is_internationalised?</a>
              </li>
          </ul>
        </dd>
        <dt>R</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-render">render</a>,
              </li>
              <li>
                <a href="#method-c-render_in">render_in</a>
              </li>
          </ul>
        </dd>
        <dt>S</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-schema">schema</a>
              </li>
          </ul>
        </dd>
        <dt>V</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-validate">validate</a>
              </li>
          </ul>
        </dd>
        <dt>W</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-walk">walk</a>
              </li>
          </ul>
        </dd>
    </dl>










    <!-- Methods -->
      <div class="sectiontitle">Class Public methods</div>
        <div class="method">
          <div class="title method-title" id="method-c-get_schema">
              <b>get_schema</b>()
            <a href="../../../classes/Hoodoo/Presenters/Base.html#method-c-get_schema" name="method-c-get_schema" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Return the schema graph. See also get_schema_definition.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-get_schema_source')" id="l_method-c-get_schema_source">show</a>
              </p>
              <div id="method-c-get_schema_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/base.rb, line 269</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">get_schema</span>
  <span class="ruby-ivar">@schema</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-c-get_schema_definition">
              <b>get_schema_definition</b>()
            <a href="../../../classes/Hoodoo/Presenters/Base.html#method-c-get_schema_definition" name="method-c-get_schema_definition" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Read back the block that defined the schema graph. See also get_schema.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-get_schema_definition_source')" id="l_method-c-get_schema_definition_source">show</a>
              </p>
              <div id="method-c-get_schema_definition_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/base.rb, line 276</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">get_schema_definition</span>
  <span class="ruby-ivar">@schema_definition</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-c-is_internationalised-3F">
              <b>is_internationalised?</b>()
            <a href="../../../classes/Hoodoo/Presenters/Base.html#method-c-is_internationalised-3F" name="method-c-is_internationalised-3F" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Does this presenter use internationalisation? Returns <code>true</code> if
so, else <code>false</code>.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-is_internationalised-3F_source')" id="l_method-c-is_internationalised-3F_source">show</a>
              </p>
              <div id="method-c-is_internationalised-3F_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/base.rb, line 263</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">is_internationalised?</span>
  <span class="ruby-ivar">@schema</span>.<span class="ruby-identifier">is_internationalised?</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-c-render">
              <b>render</b>( data, uuid = nil, created_at = nil, language = &#39;en-nz&#39; )
            <a href="../../../classes/Hoodoo/Presenters/Base.html#method-c-render" name="method-c-render" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Given some data that should conform to the subclass presenter&#39;s schema,
render it to go from the input Ruby <a href="Hash.html">Hash</a>, to an
output Ruby <a href="Hash.html">Hash</a> which will include default values
- if any - present in the schema and will drop input fields not present in
that schema. In essence, this takes data which may have been
programatically generated and sanitises it to produce valid, with-defaults
guaranteed valid output.</p>

<p><a href="Field.html">Field</a> <code>kind</code> is taken from the class
name. If concerned about clashes between resource names and <a
href="../ActiveRecord.html">ActiveRecord</a> model names, put your resource
classes inside a module for namespacing - for example:</p>

<pre><code>module Resources
  class Product &lt; Hoodoo::Presenters::Base
    schema do
      ...
    end
  end
end</code></pre>

<p>Only the class “leaf” name is used to infer the resource kind - in the
above case, that&#39;s <code>Product</code>.</p>

<p>Any field with a schema giving a default value will only appear should a
value for that field be <em>omitted</em> in the input data. If the data
provides, for example, an explicit <code>nil</code> value then a
corresponding explicit <code>nil</code> will be rendered, regardless of
defaults.</p>

<p>For belt-and-braces, unless subsequent profiling shows performance issues,
callers should call validate first to self-check their internal data
against the schema prior to rendering. That way, coding errors will be
discovered immediately, rather than hidden / obscured by the rendered
sanitisation.</p>

<p>Since rendering top-level <code>nil</code> is not valid JSON, should
<code>nil</code> be provided as input, it&#39;ll be treated as an empty
hash (“{}”) instead.</p>

<p>This is quite a low-level call. For a higher level renderer which <a
href="../../Hoodoo.html">Hoodoo</a> service resource implementations will
probably want to use for returning resource representations in responses,
see <a href="Base.html#method-c-render_in">::render_in</a>.</p>
<dl class="rdoc-list note-list"><dt><code>data</code>
<dd>
<p><a href="Hash.html">Hash</a> to be represented. <a
href="../Data.html">Data</a> within this is compared against the schema
being called to ensure that correct information is returned and unknown
data is ignored.</p>
</dd><dt><code>uuid</code>
<dd>
<p>Unique ID of the resource instance that is to be represented. If nil /
omitted, this is assumed to be a rendering of a type or other non-resource
like item. Otherwise the field is mandatory.</p>
</dd><dt><code>created_at</code>
<dd>
<p>Date/Time of instance creation. Only required if <a
href="UUID.html">UUID</a> has been provided. This is a Ruby <a
href="DateTime.html">DateTime</a> instance or similar, <em>NOT</em> a
string!</p>
</dd><dt><code>language</code>
<dd>
<p>Optional language. If the type/resource being rendered is internationalised
but this is omitted, then a value of “en-nz” is used as a default.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-render_source')" id="l_method-c-render_source">show</a>
              </p>
              <div id="method-c-render_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/base.rb, line 89</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">render</span>( <span class="ruby-identifier">data</span>, <span class="ruby-identifier">uuid</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">created_at</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">language</span> = <span class="ruby-string">&#39;en-nz&#39;</span> )
  <span class="ruby-identifier">target</span> = {}
  <span class="ruby-identifier">data</span>   = <span class="ruby-identifier">data</span> <span class="ruby-operator">||</span> {}

  <span class="ruby-ivar">@schema</span>.<span class="ruby-identifier">render</span>( <span class="ruby-identifier">data</span>, <span class="ruby-identifier">target</span> )

  <span class="ruby-comment"># Common fields are added after rendering the data in case there are</span>
  <span class="ruby-comment"># any same-named field collisions - platform defaults should take</span>
  <span class="ruby-comment"># precedence, overwriting previous definitions intentionally.</span>

  <span class="ruby-keyword">unless</span> ( <span class="ruby-identifier">uuid</span>.<span class="ruby-identifier">nil?</span> )

    <span class="ruby-identifier">raise</span> <span class="ruby-string">&quot;Can&#39;t render a Resource with a nil &#39;created_at&#39;&quot;</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">created_at</span>.<span class="ruby-identifier">nil?</span>

    <span class="ruby-comment"># Field &quot;kind&quot; is taken from the class name; this is a class method</span>
    <span class="ruby-comment"># so &quot;self.name&quot; yields &quot;Hoodoo::Data::Resources::...&quot; or similar.</span>
    <span class="ruby-comment"># We could just use &quot;split&quot;, but that creates an intermediate Array</span>
    <span class="ruby-comment"># which uses more RAM and is about half the speed (or worse) of the</span>
    <span class="ruby-comment"># following alternative (it turns out ActiveSupport 4&#39;s #demodulize</span>
    <span class="ruby-comment"># uses much the same approach).</span>

    <span class="ruby-identifier">name</span>  = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">name</span>
    <span class="ruby-identifier">index</span> = ( <span class="ruby-identifier">name</span>.<span class="ruby-identifier">rindex</span>( <span class="ruby-string">&#39;::&#39;</span> ) <span class="ruby-operator">||</span> <span class="ruby-number">-2</span> ) <span class="ruby-operator">+</span> <span class="ruby-number">2</span>
    <span class="ruby-identifier">kind</span>  = <span class="ruby-identifier">name</span>[ <span class="ruby-identifier">index</span> <span class="ruby-operator">..</span> <span class="ruby-number">-1</span> ]

    <span class="ruby-identifier">target</span>.<span class="ruby-identifier">merge!</span>( {
      <span class="ruby-string">&#39;id&#39;</span>         =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">uuid</span>,
      <span class="ruby-string">&#39;kind&#39;</span>       =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">kind</span>,
      <span class="ruby-string">&#39;created_at&#39;</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-constant">Time</span>.<span class="ruby-identifier">parse</span>( <span class="ruby-identifier">created_at</span>.<span class="ruby-identifier">to_s</span> ).<span class="ruby-identifier">utc</span>.<span class="ruby-identifier">iso8601</span>
    } )

    <span class="ruby-identifier">target</span>[ <span class="ruby-string">&#39;language&#39;</span> ] = <span class="ruby-identifier">language</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">is_internationalised?</span>()

  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">return</span> <span class="ruby-identifier">target</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-c-render_in">
              <b>render_in</b>( context, data, options = {} )
            <a href="../../../classes/Hoodoo/Presenters/Base.html#method-c-render_in" name="method-c-render_in" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>A higher level version of <a href="Base.html#method-c-render">::render</a>,
typically called from <a href="../../Hoodoo.html">Hoodoo</a> services in
their resource implementation code.</p>

<p>As with <a href="Base.html#method-c-render">::render</a>, data is rendered
according to the schema of the object the <a
href="Base.html#method-c-render_in">::render_in</a> message is sent to.
Options specify things like <a href="UUID.html">UUID</a> and created-at
date. Language information for internationalised fields can be given, but
if omitted comes from the given request context data.</p>

<p>Additional facilites exist over and above <a
href="Base.html#method-c-render">::render</a> - security scoping
information in the resource via its <code>secured_with</code> field is made
available through options (see below), along with support for embedded or
referenced resource information.</p>
<dl class="rdoc-list note-list"><dt><code>context</code>
<dd>
<p>A <a href="../Services/Context.html">Hoodoo::Services::Context</a>
instance, which is usually the value passed to a service implementation in
calls like <a
href="../Services/Implementation.html#method-i-list">Hoodoo::Services::Implementation#list</a>
or <a
href="../Services/Implementation.html#method-i-show">Hoodoo::Services::Implementation#show</a>.</p>
</dd><dt><code>data</code>
<dd>
<p><a href="Hash.html">Hash</a> to be represented. <a
href="../Data.html">Data</a> within this is compared against the schema
being called to ensure that correct information is returned and unknown
data is ignored.</p>
</dd><dt><code>options</code>
<dd>
<p>Options hash, see below.</p>
</dd></dl>

<p>The options keys are Symbols, used as follows:</p>
<dl class="rdoc-list note-list"><dt><code>uuid</code>
<dd>
<p>Same as the <code>uuid</code> parameter to <a
href="Base.html#method-c-render">::render</a>, except mandatory.</p>
</dd><dt><code>created_at</code>
<dd>
<p>Same as the <code>created_at</code> parameter to <a
href="Base.html#method-c-render">::render</a>, except mandatory.</p>
</dd><dt><code>language</code>
<dd>
<p>Optional value for resource&#39;s <code>language</code> field; taken from
the <code>context</code> parameter if omitted.</p>
</dd><dt><code>embeds</code>
<dd>
<p>A <a href="Embedding/Embeds.html">Hoodoo::Presenters::Embedding::Embeds</a>
instance that contains (fully rendered) resources which are to be embedded
in this rendered representation. Optional.</p>
</dd><dt><code>references</code>
<dd>
<p>A <a
href="Embedding/References.html">Hoodoo::Presenters::Embedding::References</a>
instance that contains UUIDs which are to be embedded in this rendered
representation as references. Optional.</p>
</dd><dt><code>secured_with</code>
<dd>
<p>An <a href="../ActiveRecord/Base.html">ActiveRecord::Base</a> subclass
instance where the model class includes a <code>secure_with</code>
declaration. As per documentation for <a
href="../ActiveRecord/Secure/ClassMethods.html#method-i-secure">Hoodoo::ActiveRecord::Secure::ClassMethods#secure</a>
and <a
href="../ActiveRecord/Secure/ClassMethods.html#method-i-secure_with">Hoodoo::ActiveRecord::Secure::ClassMethods#secure_with</a>,
this leads (potentially) to the generation of the <code>secured_with</code>
field and object value in the rendered resource data.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-render_in_source')" id="l_method-c-render_in_source">show</a>
              </p>
              <div id="method-c-render_in_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/base.rb, line 180</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">render_in</span>( <span class="ruby-identifier">context</span>, <span class="ruby-identifier">data</span>, <span class="ruby-identifier">options</span> = {} )
  <span class="ruby-identifier">uuid</span>         = <span class="ruby-identifier">options</span>[ <span class="ruby-value">:uuid</span>         ]
  <span class="ruby-identifier">created_at</span>   = <span class="ruby-identifier">options</span>[ <span class="ruby-value">:created_at</span>   ]
  <span class="ruby-identifier">language</span>     = <span class="ruby-identifier">options</span>[ <span class="ruby-value">:language</span>     ] <span class="ruby-operator">||</span> <span class="ruby-identifier">context</span>.<span class="ruby-identifier">request</span>.<span class="ruby-identifier">locale</span>
  <span class="ruby-identifier">secured_with</span> = <span class="ruby-identifier">options</span>[ <span class="ruby-value">:secured_with</span> ]
  <span class="ruby-identifier">embeds</span>       = <span class="ruby-identifier">options</span>[ <span class="ruby-value">:embeds</span>       ]
  <span class="ruby-identifier">references</span>   = <span class="ruby-identifier">options</span>[ <span class="ruby-value">:references</span>   ]

  <span class="ruby-identifier">target</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">render</span>( <span class="ruby-identifier">data</span>, <span class="ruby-identifier">uuid</span>, <span class="ruby-identifier">created_at</span>, <span class="ruby-identifier">language</span> )

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">secured_with</span>.<span class="ruby-identifier">is_a?</span>( <span class="ruby-operator">::</span><span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span> )
    <span class="ruby-identifier">result_hash</span>     = {}
    <span class="ruby-identifier">extra_scope_map</span> = <span class="ruby-identifier">secured_with</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">secured_with</span>()

    <span class="ruby-identifier">extra_scope_map</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">model_field_name</span>, <span class="ruby-identifier">key_or_options</span> <span class="ruby-operator">|</span>
      <span class="ruby-identifier">resource_field</span> = <span class="ruby-keyword">if</span> <span class="ruby-identifier">key_or_options</span>.<span class="ruby-identifier">is_a?</span>( <span class="ruby-operator">::</span><span class="ruby-constant">Hash</span> )
        <span class="ruby-keyword">next</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">key_or_options</span>[ <span class="ruby-value">:hide_from_resource</span> ] <span class="ruby-operator">==</span> <span class="ruby-keyword">true</span>
        <span class="ruby-identifier">key_or_options</span>[ <span class="ruby-value">:resource_field_name</span> ] <span class="ruby-operator">||</span> <span class="ruby-identifier">model_field_name</span>
      <span class="ruby-keyword">else</span>
        <span class="ruby-identifier">model_field_name</span>
      <span class="ruby-keyword">end</span>

      <span class="ruby-identifier">result_hash</span>[ <span class="ruby-identifier">resource_field</span>.<span class="ruby-identifier">to_s</span> ] = <span class="ruby-identifier">secured_with</span>.<span class="ruby-identifier">send</span>( <span class="ruby-identifier">model_field_name</span> )
    <span class="ruby-keyword">end</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">extra_scope_map</span>.<span class="ruby-identifier">nil?</span>

    <span class="ruby-identifier">target</span>[ <span class="ruby-string">&#39;secured_with&#39;</span> ] = <span class="ruby-identifier">result_hash</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">result_hash</span>.<span class="ruby-identifier">empty?</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">target</span>[ <span class="ruby-string">&#39;_embed&#39;</span>     ] = <span class="ruby-identifier">embeds</span>.<span class="ruby-identifier">retrieve</span>()     <span class="ruby-keyword">unless</span> <span class="ruby-identifier">embeds</span>.<span class="ruby-identifier">nil?</span>
  <span class="ruby-identifier">target</span>[ <span class="ruby-string">&#39;_reference&#39;</span> ] = <span class="ruby-identifier">references</span>.<span class="ruby-identifier">retrieve</span>() <span class="ruby-keyword">unless</span> <span class="ruby-identifier">references</span>.<span class="ruby-identifier">nil?</span>

  <span class="ruby-keyword">return</span> <span class="ruby-identifier">target</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-c-schema">
              <b>schema</b>( &amp;block )
            <a href="../../../classes/Hoodoo/Presenters/Base.html#method-c-schema" name="method-c-schema" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Define the JSON schema for validation.</p>
<dl class="rdoc-list note-list"><dt>&amp;block
<dd>
<p>Block that makes calls to the DSL defined in <a
href="BaseDSL.html">Hoodoo::Presenters::BaseDSL</a> in order to define the
schema.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-schema_source')" id="l_method-c-schema_source">show</a>
              </p>
              <div id="method-c-schema_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/base.rb, line 26</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">schema</span>( <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span> )
  <span class="ruby-ivar">@schema</span> = <span class="ruby-constant">Hoodoo</span><span class="ruby-operator">::</span><span class="ruby-constant">Presenters</span><span class="ruby-operator">::</span><span class="ruby-constant">Object</span>.<span class="ruby-identifier">new</span>
  <span class="ruby-ivar">@schema</span>.<span class="ruby-identifier">instance_eval</span>( <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span> )
  <span class="ruby-ivar">@schema_definition</span> = <span class="ruby-identifier">block</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-c-validate">
              <b>validate</b>( data, as_resource = false )
            <a href="../../../classes/Hoodoo/Presenters/Base.html#method-c-validate" name="method-c-validate" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Is the given rendering of a resource valid? Returns an array of Error
Primitive types (as hashes); this will be empty if the data given is valid.</p>
<dl class="rdoc-list note-list"><dt><code>data</code>
<dd>
<p>Ruby <a href="Hash.html">Hash</a> representation of JSON data that is to be
validated against &#39;this&#39; schema. Keys must be Strings, not Symbols.</p>
</dd><dt><code>as_resource</code>
<dd>
<p>Check Resource common fields - <code>id</code>, <code>kind</code>,
<code>created_at</code> and (for an internationalised resource)
<code>language</code>. Otherwise, only basic data schema is examined.
Optional; default is <code>false</code>.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-validate_source')" id="l_method-c-validate_source">show</a>
              </p>
              <div id="method-c-validate_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/base.rb, line 226</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">validate</span>( <span class="ruby-identifier">data</span>, <span class="ruby-identifier">as_resource</span> = <span class="ruby-keyword">false</span> )
  <span class="ruby-identifier">errors</span> = <span class="ruby-ivar">@schema</span>.<span class="ruby-identifier">validate</span>( <span class="ruby-identifier">data</span> )

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">as_resource</span>
    <span class="ruby-identifier">common_fields</span> = {
      <span class="ruby-string">&#39;id&#39;</span>         =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">data</span>[ <span class="ruby-value">:id</span>         ],
      <span class="ruby-string">&#39;created_at&#39;</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">data</span>[ <span class="ruby-value">:created_at</span> ],
      <span class="ruby-string">&#39;kind&#39;</span>       =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">data</span>[ <span class="ruby-value">:kind</span>       ]
    }

    <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">is_internationalised?</span>
      <span class="ruby-identifier">common_fields</span>[ <span class="ruby-string">&#39;internationalised&#39;</span> ] = <span class="ruby-identifier">data</span>[ <span class="ruby-string">&#39;internationalised&#39;</span> ]
      <span class="ruby-constant">Hoodoo</span><span class="ruby-operator">::</span><span class="ruby-constant">Presenters</span><span class="ruby-operator">::</span><span class="ruby-constant">CommonResourceFields</span>.<span class="ruby-identifier">get_schema</span>.<span class="ruby-identifier">properties</span>[ <span class="ruby-string">&#39;language&#39;</span> ].<span class="ruby-identifier">required</span> = <span class="ruby-keyword">true</span>
    <span class="ruby-keyword">end</span>

    <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">merge!</span>( <span class="ruby-constant">Hoodoo</span><span class="ruby-operator">::</span><span class="ruby-constant">Presenters</span><span class="ruby-operator">::</span><span class="ruby-constant">CommonResourceFields</span>.<span class="ruby-identifier">validate</span>( <span class="ruby-identifier">data</span>, <span class="ruby-keyword">false</span> ) )

    <span class="ruby-constant">Hoodoo</span><span class="ruby-operator">::</span><span class="ruby-constant">Presenters</span><span class="ruby-operator">::</span><span class="ruby-constant">CommonResourceFields</span>.<span class="ruby-identifier">get_schema</span>.<span class="ruby-identifier">properties</span>[ <span class="ruby-string">&#39;language&#39;</span> ].<span class="ruby-identifier">required</span> = <span class="ruby-keyword">false</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">return</span> <span class="ruby-identifier">errors</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-c-walk">
              <b>walk</b>( &amp;block )
            <a href="../../../classes/Hoodoo/Presenters/Base.html#method-c-walk" name="method-c-walk" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Walk the schema graph and invoke the given block on each field within it,
passing the field instances to the block for each call.</p>

<p>All fields including the top-level “root” property (which has an empty
string for a name) will be passed to the block in order of definition,
recursing into nested objects, arrays and so-on as each is encountered.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-walk_source')" id="l_method-c-walk_source">show</a>
              </p>
              <div id="method-c-walk_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/base.rb, line 256</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">walk</span>( <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span> )
  <span class="ruby-ivar">@schema</span>.<span class="ruby-identifier">walk</span>( <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span> )
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
</div>

    </div>
  </body>
</html>
